package com.ywk.miaohsa.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.security.Key;

/**
 * redis是通过Jedis对象将值存入redis里,但是jedis如何获取
 * 实现思路：redis是连接池的方式，所以是先获取连接池对象，然后通过该对象获取Jedis对象
 */
@Service
public class RedisService {

    @Autowired
    JedisPool jedisPool;

    public <T> T get(KeyPrefix keyPrefix,String key,Class<T> clazz){
        Jedis jedis=jedisPool.getResource();
        try {
            //生成正真的key
            String realKey=keyPrefix.getPrefix()+key;
            String str=jedis.get(realKey);
            T value=stringToBean(str,clazz);
            return value;
        }finally {
            returnToPool(jedis);
        }
    }

    public <T> boolean set(KeyPrefix keyPrefix,String key,T value){
        Jedis jedis=jedisPool.getResource();
        try {
            String realKey=keyPrefix.getPrefix()+key;
            String str=JSON.toJSONString(value);
            if(str ==null || str.length()<=0){
                return false;
            }
            int seconds=keyPrefix.getExpireSeconds();
            if(seconds<=0){
                jedis.set(realKey,str);
            }else{
                jedis.setex(realKey,seconds,str);
            }
            return true;
        } finally {
            returnToPool(jedis);
        }
    }

    public static <T> T stringToBean(String str,Class<T> clazz) {
        if(str == null || str.length() <= 0 || clazz == null) {
            return null;
        }
        if (clazz == int.class || clazz == Integer.class){
            return (T)Integer.valueOf(str);
        }else if(clazz == short.class || clazz == Short.class){
            return (T)Short.valueOf(str);
        }else if(clazz==String.class){
            return (T)str;
        }else if(clazz == long.class || clazz == Long.class){
            return (T)Long.valueOf(str);
        }else if(clazz==double.class || clazz == Double.class){
            return (T)Double.valueOf(str);
        }else if(clazz == float.class || clazz == Float.class){
            return (T)Float.valueOf(str);
        }else if(clazz ==char.class || clazz == Character.class){
            return (T)Character.valueOf(str.charAt(0));
        }else {
            return JSON.parseObject(str,clazz);
        }
    }

    public static <T> String beanToString(T value) {
        if(value == null) {
            return null;
        }
        Class<?> clazz = value.getClass();
        if(clazz == int.class || clazz == Integer.class) {
            return ""+value;
        }else if(clazz == String.class) {
            return (String)value;
        }else if(clazz == long.class || clazz == Long.class) {
            return ""+value;
        }else {
            return JSON.toJSONString(value);
        }
    }

    /**
     * 判断key是否存在
     * */
    public <T> boolean exists(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis =  jedisPool.getResource();
            //生成真正的key
            String realKey  = prefix.getPrefix() + key;
            return  jedis.exists(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 删除
     * */
    public boolean delete(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis =  jedisPool.getResource();
            //生成真正的key
            String realKey  = prefix.getPrefix() + key;
            long ret =  jedis.del(key);
            return ret > 0;
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 增加值
     * */
    public <T> Long incr(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis =  jedisPool.getResource();
            //生成真正的key
            String realKey  = prefix.getPrefix() + key;
            return  jedis.incr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 减少值
     * */
    public <T> Long decr(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis =  jedisPool.getResource();
            //生成真正的key
            String realKey  = prefix.getPrefix() + key;
            return  jedis.decr(realKey);
        }finally {
            returnToPool(jedis);
        }
    }

    /**
     * 释放连接
     * @param jedis
     */
    private void returnToPool(Jedis jedis) {
        if(jedis!=null){
            jedis.close();
        }
    }

    public static void main(String[] args) {
        String str="{\"prefix\":\"BasePrefix:token\",\"expireSeconds\":\"60\",\"si\":3}";
        System.out.println(JSON.parseObject(str));
        BasePrefix a=JSON.parseObject(str,BasePrefix.class);
        BasePrefix b=JSON.toJavaObject(JSON.parseObject(str),BasePrefix.class);
        System.out.println(a);
        System.out.println(b);
        BasePrefix basePrefix=new BasePrefix(60,"token");
        System.out.println(JSON.toJSONString(basePrefix));
        long i=4;
        System.out.println(JSON.toJSONString(i));
        String l="abc";
        char[] chars=l.toCharArray();
        System.out.println(Character.valueOf(chars[0]));
        for (int n=0;n<chars.length;n++){
            System.out.println(chars[n]);
        }
        int aa=JSON.parseObject("1",int.class);
        System.out.println(aa);
    }
}
